package com.example.tutormanagement.service.impl;

import com.example.tutormanagement.mapper.ChargeMapper;
import com.example.tutormanagement.mapper.RegisterMapper;
import com.example.tutormanagement.mapper.SalaryMapper;
import com.example.tutormanagement.mapper.ScheduleMapper;
import com.example.tutormanagement.pojo.*;
import com.example.tutormanagement.service.RegisterServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * &#064;Date: 23/06/14-15:37
 * &#064;Description:
 */
@Service
public class RegisterServiceImpl implements RegisterServie {

    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private ChargeMapper chargeMapper;
    @Autowired
    private SalaryMapper salaryMapper;
    @Autowired
    private ScheduleMapper scheduleMapper;

    @Override
    public List<Register> list() {
        return registerMapper.list();
    }

    @Override
    public void select_row(String id) {
        registerMapper.select_row(id);
    }

    @Override
    public List<Profession> count() {
        return registerMapper.count();
    }

    @Override
    public List<Register> search(Register register) {
        return registerMapper.search(register);
    }

    @Override
    public void update(Register register, String id) {
        register.setPid(id);
        registerMapper.update(register);
    }

    @Override
    public void insert(Register register) {
        register.setCreateTime(LocalDateTime.now());
        registerMapper.insert(register);
        // 同时添加charge salary schedule数据
        Charge charge = new Charge();
        String pid = register.getPid();
        String name = register.getName();
        charge.setPid(pid);
        charge.setName(name);
        chargeMapper.insert(charge);
        Salary salary = new Salary();
        salary.setPid(pid);
        salary.setName(name);
        salaryMapper.insert(salary);
        Schedule schedule = new Schedule();
        schedule.setPid(pid);
        schedule.setName(name);
        scheduleMapper.insert(schedule);
    }

    @Override
    public void delete(String id) {
        // 删除其它相关表
        chargeMapper.delete(id);
        salaryMapper.delete(id);
        scheduleMapper.delete(id);
        // 再删除本表数据
        registerMapper.delete(id);
    }

}
